package com.ddc.study.base;

import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created on 2018/3/28.
 *
 * @author lshun
 * @version 1.0
 */
public abstract class BasePresenter<T extends BaseView> {
    private WeakReference<T> mWeakReference;

    protected final ExecutorService mThreadPool = Executors.newFixedThreadPool(3);

    public void attachView(T view) {
        mWeakReference = new WeakReference<>(view);
    }

    protected T getView() {
        return mWeakReference.get();//获取View
    }

    protected boolean isViewAttached() {//判断是否与View建立了关联
        return mWeakReference != null && mWeakReference.get() != null;
    }

    public void detachView() {
        if (mWeakReference != null) {
            mWeakReference.clear();
            mWeakReference = null;
        }

        mThreadPool.shutdown();
    }
}
